async load reousrces
helloz 4/3/2024 Unity3d
load object functions
functions | parameter | describe |
---|---|---|
loadAsync(string,funtion) | 1.resource path 2.lua function return: UnityEngine.Object | Asynchronous loading, regardless of success or failure, the loading object will be returned. After the loading is completed, please make a judgment : IsExist(obj) It supports generics, except for sprites. |
loadAsyncSprite(string,funtion) | 1.resource path 2.lua function return: UnityEngine.Sprite | Asynchronous loading, regardless of success or failure, the loading object will be returned. After the loading is completed, please make a judgment : IsExist(obj) This is a special function, although we set the resource to '2D Sprite', the loaded type is indeed, 'Texture2D' |
load scene functions
LoadSceneMode :
LoadSceneMode.Single :Adds the Scene to the current loaded Scenes and loads a Scene.
LoadSceneMode.Additive:Adds the Scene to the current loaded Scenes.
onProgress : callback progress, the parameter is 0->1,
onCompleted:callback after loading or unloading is completed
normal
resource path: 'aaresourcesextends/scenebake/1408', Where 1408 is the scene name
functions | parameter | describe |
---|---|---|
loadSceneAsync(string,onCompleted,model) | 1.resource path 2.function onCompleted() 3. LoadSceneMode | |
loadSceneAsyncProgress(string,onProgress,onCompleted,model) | 1.resource path 2.function onProgress(_float) 3.function onCompleted() 4.LoadSceneMode |
load bake scene
scene name:"1408",By default, the scene file in the specified directory is loaded.
The baked scene files are saved in:"'aaresourcesextends/scenebake/"
version>=3318.0
functions | parameter | describe |
---|---|---|
loadBakeSceneAsync(string,onCompleted,model) | 1.scene name 2.function onCompleted() 3. LoadSceneMode | |
loadBakeSceneAsyncProgress(string,onProgress,onCompleted,model) | 1.scene name 2.function onProgress(_float) 3.function onCompleted() 4.LoadSceneMode |
unload scene functions
version>=3318.0
functions | parameter | describe |
---|---|---|
unloadSceneAsync(string,onCompleted) | 1.scene name 2.function onCompleted() | |
unloadSceneAsyncProgress(string,onProgress,onCompleted) | 1.scene name 2.function onProgress(_float) 3.function onCompleted() | --Progress:_float = 0->1 function onProgress(_float) end |
get and check scene
functions | parameter | describe |
---|---|---|
getSceneByName(string) | 1.scene name 2.return scene | scene:UnityEngine.SceneManagement.Scene |
hasSceneByName(string) | 1.scene name 2.return bool | if hasSceneByName("1408") == true then print("have") else print("no") end |
moveGoToSceneByName(GameObject,string) | 1. GameObject 2. scene name 3. return bool | Place the gameobject under the specified scene hierachy (把gameobject 放在指定的场景hierachy下) |
example:
load object
- Load an FBX/prefab
- Load an animation controller and set the animation controller to the game object
- Load a 2D Sprite
isloaded =false
function Update()
if isResReady() and isloaded==false then
isloaded =true
log('isResReady')
loadAsync("Windmill_Blue",onLoadFinished)
loadAsync("Animation/Animator/AvatarV2Fbx",onLoadFinished2)
loadAsyncSprite("UI/UIicon/HeroCharacter/Gritster",onLoadFinished4)
end
end
function onLoadFinished4(obj)
log('onLoadFinished4')
if IsExist(obj) then
print(obj)--LUA: Gritster (UnityEngine.Sprite)
end
end
function onLoadFinished(prefab)
log('onLoadFinished')
if IsExist(prefab) then
local go = newObject(prefab)
go.transform.position =Vector3(10,2,4)
end
end
function onLoadFinished2(prefab)
log('onLoadFinished2: '..prefab.name)
if IsExist(prefab) then
avatar = newObject(prefab):GetComponent(typeof(UnityEngine.Animator))
--Generic loading, loads a character animation controller
--(泛型加载,加载一个角色动画控制器) AvatarV2FbxController.controller
loadAsync("Animation/Animator/AvatarV2Fbx/AvatarV2FbxController",onLoadFinished3)
else
LogError('nil')
end
end
function onLoadFinished3(obj)
log('onLoadFinished3: '..obj.name)
if IsExist(obj) then
avatar.runtimeAnimatorController = obj
else
LogError('nil')
end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
load scene
--[[
LoadSceneMode has two types
LoadSceneMode.Additive
LoadSceneMode.Single
]]
function onCompleted()
print("load completed")
end
--Progress:_float = 0->0.x->1
function onProgress(_float)
print("progress",_float)
end
--Method 1
loadSceneAsync('aaresourcesextends/scenebake/1408',onCompleted,LoadSceneMode.Additive)
--Method 2
loadSceneAsyncProgress('aaresourcesextends/scenebake/1408',onProgress,onComplte,LoadSceneMode.Additive)
--Method 3
loadBakeSceneAsync("1408",onCompleted,LoadSceneMode.Additive)
--Method 4
loadBakeSceneAsyncProgress("1408",onProgress,onComplte,LoadSceneMode.Additive)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
unload scene
Asynchronous uninstall
function onCompleted()
print("unload completed")
end
--Progress:_float = 0->1
function onProgress(_float)
print("unload progress",_float)
end
--Method 1:Progress and Complte call back
unloadSceneAsyncProgress("1408",onProgress,onCompleted)
--Method 2:Complte call back
unloadSceneAsync("1408",onCompleted)
--Method 3:no call back
unloadSceneAsync("1408")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
scene functions
if hasSceneByName("1408") == true then
print("have")
else
print("no")
end
local scene =getSceneByName(sceneName);
if scene.name==nil or scene.name=="" then
print("no")
end
--Move the object to the specified scene
local go = GameObject("test")
moveGoToSceneByName(go,"6851") -- GameObject,scene name.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14